package org.fao.pims.service.pollneeds;

import java.util.List;
import org.fao.pims.model.*;
import org.fao.pims.persistence.*;

public class PollinationNeedsManagerImpl implements PollinationNeedsManager {
	
	public PollinationNeed getPollinationNeed(String name) throws PollinationNeedsException {
		
		PollinationNeed pollNeed = null;
		Taxon crop = null;				
		
		TaxonDAO dao = new TaxonDAOImpl();
		DocumentDao docDao = new DocumentDaoImpl();
		
		PollinationNeedDao pollDao = new PollinationNeedDaoImpl();				
		
		System.out.println("scientific name selected: " + name);
		
		try {
			crop = dao.getTaxonByScientificName(name);
			
			System.out.println("common name: " + crop.getCommonName());
			
			// get the pollination needs of crop
			pollNeed = pollDao.getPollinationNeed(crop);
			// get the pollinators of crop
			List cropPollinators = dao.getPollinatorsOfCrop(crop);
			// get the bibliographic references (documents)
			List documents = docDao.getDocumentsOfPollNeed(pollNeed);
			// add the crop data to the pollination needs
			pollNeed.setCrop(crop);
			// add the pollinators to the pollination needs
			pollNeed.setPollinator(cropPollinators);		
			// add the bibliographic references to the pollination needs
			pollNeed.setSourceList(documents);
		}
		catch(DataAccessException dae) {
			
			System.out.println(dae.getMessage());
			
			throw new PollinationNeedsException();
			
		}
		
		if(pollNeed != null) {
			
			return pollNeed;
			
		}
		else {
		
			throw new PollinationNeedsException();
			
		}
			
		
	}

}
